﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.Util;
using Server.Models;
using Newtonsoft.Json;

namespace Server.CompanyService
{
    public class CompanyDAL
    {
        private CacheUtil cacheCtrl = new CacheUtil();
        private DBUtil dbCtrl = new DBUtil();
        #region 公司
        public void CompanyAdd(ManagerCompany company)
        {
            try
            {
                string sqlstr = string.Format(" insert into Manage_Company(CName) values('{0}') ", company.CName);
                dbCtrl.Exec(sqlstr);
                CacheCompanyList();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CompanyEdit(ManagerCompany company)
        {
            try
            {
                string sqlstr = string.Format(" update Manage_Company set CName = '{0}' ", company.CName);
                dbCtrl.Exec(sqlstr);
                CacheCompanyList();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CompanyDel(int id)
        {
            try
            {
                string sqlstr = "delete from Manage_Company where id =" + id;
                dbCtrl.Exec(sqlstr);
                CacheCompanyList();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerCompany[] GetCompanyList()
        {
            try
            {
                string tmpStr = (string)cacheCtrl.CacheGet("Manage_Company:List");
                if (string.IsNullOrEmpty(tmpStr))
                    return null;
                tmpStr = JsonConvert.DeserializeObject<string>(tmpStr);
                if (string.IsNullOrEmpty(tmpStr))
                    return null;
                ManagerCompany[] mc = JsonConvert.DeserializeObject<ManagerCompany[]>(tmpStr);
                return mc;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ManagerCompany GetCompanyByName(string name)
        {
            try
            {
                string sqlstr = string.Format(" select * from Manage_Company where CName like '%{0}%' ", name);
                var dt = dbCtrl.Query(sqlstr);
                ManagerCompany mc = new ManagerCompany();
                mc.id = Convert.ToInt32(dt.Rows[0][0]);
                mc.CName = dt.Rows[0][1].ToString();
                return mc;
            }
            catch (Exception)
            {
                throw;
            }
        }
        /// <summary>
        /// 缓存公司列表
        /// </summary>
        public void CacheCompanyList()
        {
            try
            {
                string sqlstr = " select * from Manage_Company ";
                var dt = dbCtrl.Query(sqlstr);
                ManagerCompany[] mc = new ManagerCompany[dt.Rows.Count];
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    mc[i] = new ManagerCompany
                    {
                        id = Convert.ToInt32(dt.Rows[i]["id"]),
                        CName = dt.Rows[i]["CName"].ToString()
                    };
                }
                cacheCtrl.CacheSet("Manage_Company:List", mc);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
        #region 数据库读地区
        public void CacheArea()
        {
            try
            {
                string sqlstr = "select * from PT_AreaProvince";
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    throw new Exception("未知错误！");
                //读所有地区信息
                List<AreaProvinceModel> pList = new List<AreaProvinceModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    pList.Add(new AreaProvinceModel
                    {
                        CityList = getCityFromDB(Convert.ToInt32(dr["id"])),
                        Id = Convert.ToInt32(dr["id"]),
                        PName = dr["FName"].ToString()
                    });
                }
                //写缓存
                cacheCtrl.CacheSet("AR:PR:LIST", pList, 86400);
                for (int i = 0; i < pList.Count; i++)
                {
                    //单个省
                    cacheCtrl.CacheSet("AR:PR:ID:" + pList[i].Id, pList[i]);
                    //市列表
                    cacheCtrl.CacheSet("AR:CT:LIST:" + pList[i].Id, pList[i].CityList, 86400);
                    //单个市
                    for (int j = 0; j < pList[i].CityList.Length; j++)
                    {
                        var ct = pList[i].CityList[j];
                        //单个市
                        cacheCtrl.CacheSet("AR:CT:ID:" + ct.Id, ct, 86400);
                        //区县列表
                        cacheCtrl.CacheSet("AR:DT:LIST:" + ct.Id, ct.DistrictList);
                        //单个区县
                        for (int k = 0; k < ct.DistrictList.Length; k++)
                        {
                            var vdt = ct.DistrictList[i];
                            cacheCtrl.CacheSet("AR:DT:ID:" + vdt.Id, vdt, 86400);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private AreaCityModel[] getCityFromDB(int pid)
        {
            try
            {
                string sqlstr = "select * from PT_AreaCity where Pid=" + pid;
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    return null;
                List<AreaCityModel> cList = new List<AreaCityModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    cList.Add(new AreaCityModel
                    {
                        Id = Convert.ToInt32(dr["id"]),
                        Name = dr["FName"].ToString(),
                        DistrictList = getDistrictFromDB(Convert.ToInt32(dr["id"])),
                        Pid = Convert.ToInt32(dr["Pid"])
                    });
                }
                return cList.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        private AreaDistrictModel[] getDistrictFromDB(int cId)
        {
            try
            {
                string sqlstr = "select * from PT_AreaDistrict where Cid=" + cId;
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    return null;
                List<AreaDistrictModel> dList = new List<AreaDistrictModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    dList.Add(new AreaDistrictModel
                    {
                        Cid = Convert.ToInt32(dr["Cid"]),
                        Id = Convert.ToInt32(dr["Id"]),
                        Name = dr["FName"].ToString()
                    });
                }
                return dList.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 数据库读税务机关
        public void CacheTaxArea()
        {
            try
            {
                string sqlstr = "select * from PT_SBProvince";
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    throw new Exception("未知错误！");
                //读所有地区信息
                List<SBProvinceModel> pList = new List<SBProvinceModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    pList.Add(new SBProvinceModel
                    {
                        CityList = getSBCityFromDB(Convert.ToInt32(dr["id"])),
                        Id = Convert.ToInt32(dr["id"]),
                        Code = dr["FCode"].ToString(),
                        Name = dr["FName"].ToString()
                    });
                }
                //写缓存
                cacheCtrl.CacheSet("AR:SBPR:LIST", pList, 86400);
                for (int i = 0; i < pList.Count; i++)
                {
                    //单个省
                    cacheCtrl.CacheSet("AR:SBPR:ID:" + pList[i].Id, pList[i]);
                    //市列表
                    cacheCtrl.CacheSet("AR:SBCT:LIST:" + pList[i].Id, pList[i].CityList, 86400);
                    //单个市
                    for (int j = 0; j < pList[i].CityList.Length; j++)
                    {
                        var ct = pList[i].CityList[j];
                        //单个市
                        cacheCtrl.CacheSet("AR:SBCT:ID:" + ct.Id, ct, 86400);
                        //区县列表
                        cacheCtrl.CacheSet("AR:SBDT:LIST:" + ct.Id, ct.DistrictList);
                        //单个区县
                        for (int k = 0; k < ct.DistrictList.Length; k++)
                        {
                            var vdt = ct.DistrictList[i];
                            cacheCtrl.CacheSet("AR:SBDT:ID:" + vdt.Id, vdt, 86400);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private SBCityModel[] getSBCityFromDB(int pid)
        {
            try
            {
                string sqlstr = "select * from PT_SBCity where Pid=" + pid;
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    return null;
                List<SBCityModel> cList = new List<SBCityModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    cList.Add(new SBCityModel
                    {
                        Id = Convert.ToInt32(dr["id"]),
                        Name = dr["FName"].ToString(),
                        Code = dr["FCode"].ToString(),
                        DistrictList = getSBDistrictFromDB(Convert.ToInt32(dr["id"])),
                        Pid = Convert.ToInt32(dr["Pid"])
                    });
                }
                return cList.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        private SBDistrictModel[] getSBDistrictFromDB(int cId)
        {
            try
            {
                string sqlstr = "select * from PT_SBDistrict where Cid=" + cId;
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    return null;
                List<SBDistrictModel> dList = new List<SBDistrictModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    dList.Add(new SBDistrictModel
                    {
                        Cid = Convert.ToInt32(dr["Cid"]),
                        Id = Convert.ToInt32(dr["Id"]),
                        Code = dr["FCode"].ToString(),
                        Name = dr["FName"].ToString()
                    });
                }
                return dList.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 数据库读行业
        public void CacheTrade()
        {
            try
            {
                string sqlstr = "select * from PT_TradeBig";
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    throw new Exception("未知错误！");
                //读所有地区信息
                List<TradeBigModel> bList = new List<TradeBigModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    bList.Add(new TradeBigModel
                    {
                        SubTrades = getMidFromDB(Convert.ToInt32(dr["id"])),
                        id = Convert.ToInt32(dr["id"]),
                        TCode = dr["TCode"].ToString(),
                        TName = dr["TName"].ToString()
                    });
                }
                //写缓存
                cacheCtrl.CacheSet("AR:TradeBig:LIST", bList, 86400);
                for (int i = 0; i < bList.Count; i++)
                {
                    //单个省
                    cacheCtrl.CacheSet("AR:TradeBig:ID:" + bList[i].id, bList[i]);
                    //市列表
                    cacheCtrl.CacheSet("AR:TradeMid:LIST:" + bList[i].id, bList[i].SubTrades, 86400);
                    //单个市
                    for (int j = 0; j < bList[i].SubTrades.Length; j++)
                    {
                        var ct = bList[i].SubTrades[j];
                        //单个市
                        cacheCtrl.CacheSet("AR:TradeMid:ID:" + ct.id, ct, 86400);
                        //区县列表
                        cacheCtrl.CacheSet("AR:TradeSmall:LIST:" + ct.id, ct.SubTrades);
                        //单个区县
                        for (int k = 0; k < ct.SubTrades.Length; k++)
                        {
                            var vdt = ct.SubTrades[i];
                            cacheCtrl.CacheSet("AR:TradeSmall:ID:" + vdt.id, vdt, 86400);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        private TradeMidModel[] getMidFromDB(int bid)
        {
            try
            {
                string sqlstr = "select * from PT_SBCity where Bid=" + bid;
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    return null;
                List<TradeMidModel> cList = new List<TradeMidModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    cList.Add(new TradeMidModel
                    {
                        id = Convert.ToInt32(dr["id"]),
                        TName = dr["TName"].ToString(),
                        TCode = dr["TCode"].ToString(),
                        SubTrades = getSmallFromDB(Convert.ToInt32(dr["id"])),
                        Bid = Convert.ToInt32(dr["Bid"])
                    });
                }
                return cList.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        private TradeSmallModel[] getSmallFromDB(int mId)
        {
            try
            {
                string sqlstr = "select * from PT_TradeSmall where Mid=" + mId;
                var dt = dbCtrl.Query(sqlstr);
                if (null == dt)
                    return null;
                List<TradeSmallModel> dList = new List<TradeSmallModel>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    var dr = dt.Rows[i];
                    dList.Add(new TradeSmallModel
                    {
                        Mid = Convert.ToInt32(dr["Mid"]),
                        id = Convert.ToInt32(dr["Id"]),
                        TCode = dr["TCode"].ToString(),
                        TName = dr["TName"].ToString()
                    });
                }
                return dList.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 地区
        public void ProvinceAdd(AreaProvinceModel province)
        {
            try
            {
                string sqlstr = string.Format(" insert into PT_AreaProvince(PName) values('{0}')", province.PName);
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ProvinceEdit(AreaProvinceModel Province)
        {
            try
            {
                string sqlstr = string.Format(" update PT_AreaProvince set PName = '{0}' where id = {1} ", Province.PName, Province.Id);
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ProvinceDel(int id)
        {
            try
            {
                string sqlstr = string.Format(" delect from PT_AreaProvince where id = {0}", id);
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AreaProvinceModel[] GetProvinceList()
        {
            try
            {
                AreaProvinceModel[] pList = new AreaProvinceModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:PR:LIST");
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheArea();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:PR:LIST");
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    pList = JsonConvert.DeserializeObject<AreaProvinceModel[]>(tmpstr);
                }
                return pList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void CityAdd(AreaCityModel City)
        {
            try
            {
                string sqlstr = string.Format(" insert into PT_AreaCity(Name,Pid) values('{0}',{1}) ", City.Name, City.Pid);
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CityEdit(AreaCityModel City)
        {
            try
            {
                string sqlstr = string.Format(" update PT_AreaCity set Name = '{0}',Pid = {1} where id = {2} ", City.Name, City.Pid, City.Id);
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void CityDel(int id)
        {
            try
            {
                string sqlstr = " delect from PT_AreaCity where id =  " + id;
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AreaCityModel[] GetCityList(int pId)
        {
            try
            {
                AreaCityModel[] cList = new AreaCityModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:CT:LIST:" + pId);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheArea();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:CT:LIST:" + pId);
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    cList = JsonConvert.DeserializeObject<AreaCityModel[]>(tmpstr);
                }
                return cList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void DistrictAdd(AreaDistrictModel District)
        {
            try
            {
                string sqlstr = " insert into PT_AreaDistrict(Name,Cid) values(Name,Cid)";
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void DistrictEdit(AreaDistrictModel District)
        {
            try
            {
                string sqlstr = string.Format(" update PT_AreaDistrict Name = '{0}',Cid = {1} where id = {2}", District.Name, District.Cid, District.Id);
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void DistrictDel(int id)
        {
            try
            {
                string sqlstr = " delete from PT_AreaDistrice where id = " + id;
                dbCtrl.Exec(sqlstr);
                CacheArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public AreaDistrictModel[] GetDistrictList(int cId)
        {
            try
            {
                AreaDistrictModel[] dList = new AreaDistrictModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:DT:LIST:" + cId);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheArea();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:DT:LIST:" + cId);
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    dList = JsonConvert.DeserializeObject<AreaDistrictModel[]>(tmpstr);
                }
                return dList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 税务机关
        public void SBProvinceAdd(SBProvinceModel SBProvince)
        {
            try
            {
                string sqlstr = string.Format(" insert into PT_SBProvince(Code,Name) values('{0}','{1}') ", SBProvince.Code, SBProvince.Name);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SBProvinceEdit(SBProvinceModel SBProvince)
        {
            try
            {
                string sqlstr = string.Format(" update PT_SBProvince set Code = '{0}' , Name = '{1}' where id = {2} ", SBProvince.Code, SBProvince.Name, SBProvince.Id);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SBProvinceDel(int id)
        {
            try
            {
                string sqlstr = " delete from PT_SBProvince where id = " + id;
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public SBProvinceModel[] GetSBProvinceList()
        {
            try
            {
                SBProvinceModel[] pList = new SBProvinceModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:SBPR:LIST");
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheArea();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:SBPR:LIST");
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    pList = JsonConvert.DeserializeObject<SBProvinceModel[]>(tmpstr);
                }
                return pList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void SBCityAdd(SBCityModel SBCity)
        {
            try
            {
                string sqlstr = string.Format(" insert into PT_SBCity(Code,Name,Pid) values('{0}','{1}',{2}) ", SBCity.Code, SBCity.Name, SBCity.Id);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SBCityEdit(SBCityModel SBCity)
        {
            try
            {
                string sqlstr = string.Format(" update PT_SBCity set Code = '{0}',Name = '{1}',Pid = {2} where id = {3}", SBCity.Code, SBCity.Name, SBCity.Pid, SBCity.Id);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SBCityDel(int id)
        {
            try
            {
                string sqlstr = " delete from PT_SBCity where id = " + id;
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public SBCityModel[] GetSBCityList(int pId)
        {
            try
            {
                SBCityModel[] cList = new SBCityModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:SBCT:LIST:" + pId);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheArea();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:SBCT:LIST:" + pId);
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    cList = JsonConvert.DeserializeObject<SBCityModel[]>(tmpstr);
                }
                return cList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public void SBDistrictAdd(SBDistrictModel SBDistrict)
        {
            try
            {
                string sqlstr = string.Format("insert into PT_SBDistrict(Name,Code,Cid) values('{0}','{1}',{2})", SBDistrict.Name, SBDistrict.Code, SBDistrict.Cid);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SBDistrictEdit(SBDistrictModel SBDistrict)
        {
            try
            {
                string sqlstr = string.Format("update PT_SBDistrict set Name = '{0}',Code = '{1}',Cid = {2} where id = {3}", SBDistrict.Name, SBDistrict.Code, SBDistrict.Cid, SBDistrict.Id);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void SBDistrictDel(int id)
        {
            try
            {
                string sqlstr = "delete from PT_SBDistrict where id = " + id;
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public SBDistrictModel[] GetSBDistrictList(int cId)
        {
            try
            {
                SBDistrictModel[] dList = new SBDistrictModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:SBDT:LIST:" + cId);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheArea();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:SBDT:LIST:" + cId);
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    dList = JsonConvert.DeserializeObject<SBDistrictModel[]>(tmpstr);
                }
                return dList;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
        #region 行业
        public void TradeBigAdd(TradeBigModel TradeBig)
        {
            try
            {
                string sqlstr = string.Format("insert into PT_TradeBig(TCode,TName) values('{0}','{1}')", TradeBig.TCode, TradeBig.TName);
                dbCtrl.Exec(sqlstr);
                CacheTrade();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeBigEdit(TradeBigModel TradeBig)
        {
            try
            {
                string sqlstr = string.Format("update PT_TradeBig set TCode = '{0}',TName = '{1}' where id = {2}", TradeBig.TCode, TradeBig.TName, TradeBig.id);
                dbCtrl.Exec(sqlstr);
                CacheTrade();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeBigDel(int id)
        {
            try
            {
                string sqlstr = "delete from PT_TradeBig where id = " + id;
                dbCtrl.Exec(sqlstr);
                CacheTrade();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public TradeBigModel[] GetTradeBigList()
        {
            try
            {
                TradeBigModel[] pList = new TradeBigModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:TradeBig:LIST");
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheTrade();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:TradeBig:LIST");
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    pList = JsonConvert.DeserializeObject<TradeBigModel[]>(tmpstr);
                }
                return pList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeMidAdd(TradeMidModel TradeMid)
        {
            try
            {
                string sqlstr = string.Format("insert into PT_TradeMid(TCode,TName,Bid) values('{0}','{1}',{2})", TradeMid.TCode, TradeMid.TName, TradeMid.Bid);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeMidEdit(TradeMidModel TradeMid)
        {
            try
            {
                string sqlstr = string.Format("update PT_TradeMid set TCode = '{0}',TName = '{1}',Bid={2} where id = {3}", TradeMid.TCode, TradeMid.TName, TradeMid.Bid, TradeMid.id);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeMidDel(int id)
        {
            try
            {
                string sqlstr = "delete from PT_TradeMid where id =" + id;
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public TradeMidModel[] GetTradeMidList(int bId)
        {
            try
            {
                TradeMidModel[] cList = new TradeMidModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:TradeMid:LIST:" + bId);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheTrade();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:TradeMid:LIST:" + bId);
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    cList = JsonConvert.DeserializeObject<TradeMidModel[]>(tmpstr);
                }
                return cList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeSmallAdd(TradeSmallModel TradeSmall)
        {
            try
            {
                string sqlstr = string.Format(" insert into PT_TradeSmall(TCode,TNAme,Mid) values('{0}','{1}',{2})", TradeSmall.TCode, TradeSmall.TName, TradeSmall.Mid);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeSmallEdit(TradeSmallModel TradeSmall)
        {
            try
            {
                string sqlstr = string.Format(" update PT_TradeSmall TCode = '{0}' ,TName = '{1}', Mid = {2} where id = {3} ", TradeSmall.TCode, TradeSmall.TName, TradeSmall.Mid, TradeSmall.id);
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void TradeSmallDel(int id)
        {
            try
            {
                string sqlstr = " delete from  PT_TradeSmall where id =  " + id;
                dbCtrl.Exec(sqlstr);
                CacheTaxArea();
            }
            catch (Exception)
            {
                throw;
            }
        }
        public TradeSmallModel[] GetTradeSmallList(int mId)
        {
            try
            {
                TradeSmallModel[] dList = new TradeSmallModel[] { };
                string tmpstr = (string)cacheCtrl.CacheGet("AR:TradeSmall:LIST:" + mId);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    CacheTrade();
                    tmpstr = (string)cacheCtrl.CacheGet("AR:TradeSmall:LIST:" + mId);
                }
                tmpstr = JsonConvert.DeserializeObject<string>(tmpstr);
                if (string.IsNullOrEmpty(tmpstr))
                {
                    return null;
                }
                else
                {
                    dList = JsonConvert.DeserializeObject<TradeSmallModel[]>(tmpstr);
                }
                return dList;
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion
    }
}

